﻿using System;
using RestSharp;
using Microsoft.SystemCenter.Orchestrator.Integration;
using System.Collections.Generic;
using SocialMediaIntegrationPack.Properties;

namespace SocialMediaIntegrationPack
{
    [Activity("PostYammerMessage")]
    public class YammerPostMessageAct : YammerActivity
    {
        [ActivityInput("Text")]
        public string Text { get; set; }

        private HashSet<string> needID = new HashSet<string> { "group_id", "replied_to_id", "direct_to_id" };

        [ActivityInput("IDType", Default = "None", Options = "None,group_id,replied_to_id,direct_to_id")]
        public string IdType { get; set; }

        private static readonly string apiResourceName = "api/v1/messages.json";

        [ActivityMethod]
        public void Execute()
        {
            if (String.IsNullOrEmpty(Text))
            {
                throw new ArgumentException(Resources.StatusMinLengthError);
            }

            var client = GetClient(Configuration.AccessToken, Configuration.AccessTokenSecret);
            var request = new RestRequest(apiResourceName, Method.POST);
            Messages = new List<YammerMessage>();

            request.AddParameter("body", Text);

            if (needID.Contains(IdType))
            {
                CheckId();
                request.AddParameter(IdType, Id);
            }

            var r = client.Execute<YammerMessages>(request);

            CheckResponse(r);

            Response = r.StatusDescription;
            if (r.Data != null && r.Data.Messages != null)
                Messages.AddRange(r.Data.Messages);
        }

    }
}